import pygame

pygame.init()

window = pygame.display.set_mode((400, 600))
pygame.display.set_caption('键盘事件')

window.fill((255, 255, 255))

pygame.display.flip()

t_x = 10

font = pygame.font.Font('files/AlimamaDaoLiTi.ttf', 30)

while True:
    for event in pygame.event.get():

        """
            KEYDOWN         -- 按下
            KEYUP           -- 弹起
        """

        if event.type == pygame.QUIT:
            exit()

        # 键盘按下
        if event.type == pygame.KEYDOWN:
            # print(event.key)  该键对应的编码值
            # print(chr(event.key))   将码值转为对应的键盘字符
            text = font.render(chr(event.key), True, (0, 0, 0))
            window.blit(text, (t_x,20))
            t_x += 20
            pygame.display.update()
